package server.child.thread;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import qq.message.DataPackage;
import server.child.thread.handler.ChildThreadHandle;
import server.handler.ServerHandler;

public class ServerChildThread implements Runnable {

	private Socket socket;
	private OutputStream os;
	private InputStream is;
	private ObjectInputStream ois;
	private ObjectOutputStream oos;
	private String name;


	public void run() {
		// TODO Auto-generated method stub
		//open port
		System.out.println(this.name+" is running");
		DataPackage message = null;
		ChildThreadHandle chh=new ChildThreadHandle();
		while(true){
				try {
						message=(DataPackage)ois.readObject();
//						System.out.println("Thread 1 deal "+message.getFromName()+" by type "+message.getMessageType());
						handlerMessage(message);
//					System.out.println("Thread 1 sleep");
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
	}
	
	//当客户端异常关闭的时候处理
	private void downThread() throws IOException {
		// TODO Auto-generated method stub
		ChildThreadHandle chh=new ChildThreadHandle();
		DataPackage dp=new DataPackage();
		dp.setFromName(name);
		chh.handleLogout(dp);
	}

	public void handlerMessage(DataPackage message) throws IOException {
		// TODO Auto-generated method stub
		ChildThreadHandle chh=new ChildThreadHandle();
		switch(message.getMessageType())
		{
		 case  LOGIN : chh.handleLogin(message);  //理论不出现
			 break;
		 case LOGOUT : chh.handleLogout(message);
			 break;
		 case LIST   : chh.handleList(message);   //理论不出现
			 break;
		 case MESSAGE: chh.handleMessage(message);
			 break;
		 case SHAKE  : chh.handleShake(message);
			 break;
		 default:
			 break;
		}
	}


	public Socket getSocket() {
		return socket;
	}


	public void setSocket(Socket socket) {
		this.socket = socket;
	}


	public OutputStream getOs() {
		return os;
	}


	public void setOs(OutputStream os) {
		this.os = os;
	}


	public InputStream getIs() {
		return is;
	}


	public void setIs(InputStream is) {
		this.is = is;
	}


	public ObjectInputStream getOis() {
		return ois;
	}


	public void setOis(ObjectInputStream ois) {
		this.ois = ois;
	}


	public ObjectOutputStream getOos() {
		return oos;
	}


	public void setOos(ObjectOutputStream oos) {
		this.oos = oos;
	}


	/**
	 * @param socket
	 * @param os
	 * @param is
	 * @param ois
	 * @param oos
	 */
	public ServerChildThread(String name,Socket socket, OutputStream os, InputStream is,
			ObjectInputStream ois, ObjectOutputStream oos) throws IOException{
		super();
		this.name=name;
		this.socket = socket;
		this.os = os;
		this.is = is;
		this.ois = ois;
		this.oos = oos;
	}

	
}
